package com.chb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.DictTypeMapper;
import com.chb.system.service.DictTypeService;
import com.chb.system.vo.DictTypeVo;
import com.hos.entity.sys.DictType;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ：张家琪
 * @date ：Created in 2023/12/15 10:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {
    @Resource
    private DictTypeMapper dictTypeMapper;

    /**
     * 条件查询字典类型信息并进行分页
     *
     * @param dictTypeVo
     * @return
     */
    @Override
    public Page<DictType> findDictTypeByCond(DictTypeVo dictTypeVo) {
        Page<DictType> page = new Page<>(dictTypeVo.getPageNum(), dictTypeVo.getPageSize());
        LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(dictTypeVo.getDictName())) {
            wrapper.like(DictType::getDictName, dictTypeVo.getDictName());
        }
        if (StringUtils.hasText(dictTypeVo.getDictType())) {
            wrapper.like(DictType::getDictType, dictTypeVo.getDictType());
        }
        if (StringUtils.hasText(dictTypeVo.getStatus())) {
            wrapper.eq(DictType::getStatus, dictTypeVo.getStatus());
        }
        if (Objects.nonNull(dictTypeVo.getDateRange())) {
            wrapper.between(DictType::getCreateTime, dictTypeVo.getDateRange()[0], dictTypeVo.getDateRange()[1]);
        }
        return dictTypeMapper.selectPage(page, wrapper);
    }

    /**
     * 查询所有的可用字典类型
     *
     * @return
     */
    @Override
    public List<DictType> selectAllDictType() {
        LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictType::getStatus, 0);
        return dictTypeMapper.selectList(wrapper);
    }

    // 增加
    @Override
    public Integer addDictType(DictType dictType) {
        // 创建时间
        dictType.setCreateTime(new Date());
        return dictTypeMapper.insert(dictType);
    }

    // 删除，批量删除
    @Override
    public Integer deleteDiceTypeByIds(List<Long> ids) {
        return dictTypeMapper.deleteBatchIds(ids);
    }

    // 根据子字典编号ID查询到一个字典类型信息
    @Override
    public DictType getDictTypeById(Long dictTypeId) {
        return dictTypeMapper.selectById(dictTypeId);
    }

    // 修改
    @Override
    public Integer updateDict(DictType dictType) {
        return dictTypeMapper.updateById(dictType);
    }
}